<script>
import {employmentType} from "@/constant/employees";

export default {
  name: "userInfo",
  data() {
    return {
      /* 表单数据对象 */
      formData: {
        workNumber: '',
        username: '',
        mobile: '',
        timeOfEntry: '',
        correctionTime: '',
        departmentName: '',
        formOfEmployment: '',
        staffPhoto: '',

      },
      /* 表单规则对象 */
      formRules: {},
      employmentType
    }
  },
  methods: {
    /* findUserDetail()函数的作用根据员工用户的id派发action获取当前员工详情 */
    async findUserDetail() {
      // 派发action获取用户信息
      const user = await this.$store.dispatch('employeesStore/findUserDetail', this.$route.params.id)
      // 数据回显
      this.formData = user
    },
    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      try {
        const message = await this.$store.dispatch('employeesStore/updateUserInfo', this.formData)
        this.$message.success(message)
        // 重新派发action获取用户信息
        // this.$store.dispatch('user/getUserInfoAction')
        this.$router.back()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  mounted() {
    /* 组件挂载完毕调用findUserDetail()派发action获取员工详情 */
    this.findUserDetail()
  }
}
</script>

<template>
  <el-form :model="formData" :rules="formRules" label-width="100px">
    <el-form-item label="工号">
      <el-input placeholder="请输入工号" v-model="formData.workNumber"></el-input>
    </el-form-item>
    <el-form-item label="姓名">
      <el-input placeholder="请输入姓名" v-model="formData.username"></el-input>
    </el-form-item>
    <el-form-item label="手机">
      <el-input placeholder="请输入手机号" v-model="formData.mobile"></el-input>
    </el-form-item>
    <el-form-item label="入职时间">
      <el-date-picker type="date" placeholder="请选择入职日期" v-model="formData.timeOfEntry"></el-date-picker>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker type="date" placeholder="请选择转正日期" v-model="formData.correctionTime"></el-date-picker>
    </el-form-item>
    <el-form-item label="部门">
      <el-input placeholder="请输入部门" v-model="formData.departmentName"></el-input>
    </el-form-item>
    <el-form-item label="聘用形式">
      <el-select placeholder="请选择聘用形式" v-model="formData.formOfEmployment">
        <el-option v-for="(item,index) in employmentType" :key="item.id" :value="item.id" :label="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="员工头像">
      <upload-avatar v-model="formData.staffPhoto"></upload-avatar>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" icon="el-icon-check" @click="handleSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped lang="scss">
.el-form {
  width: 20%;

  .el-select {
    width: 100%;
  }

  .el-date-editor {
    width: 100%;
  }
}
</style>
